<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>（转）Spring Security 3.1 自定义实例之登陆 - iLife - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=5bITb1XmtieJKhNy3HCsng1mgkC1fjWAtqCxIQA888c1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="http://common.cnblogs.com/Skins/Cogitation/style.css?id=20131027"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/ilife/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/ilife/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/ilife/wlwmanifest.xml"/>
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'ilife';</script>
<script src="/bundles/blog-common.js?v=-V_N633b1XulUO78It1JZ2h-pQ7JK_QgceUBv-24X2s1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<div id="top">
	
<div>
	<table>
		<tr>
			<td class="HeaderTitles">
				<h1 class="HeaderTitle"><a id="Header1_HeaderTitle" class="HeaderMainTitle" href="http://www.cnblogs.com/ilife/">iLife</a></h1>
				<p id="tagline"></p>
			</td>
		</tr>
	</table>
</div>
<div class="HeaderBar">
	<table id="HeaderBar" class="HeaderBar" cellpadding="0" cellspacing="0">
		<tr>
			<td class="HeaderBarTab" nowrap>
&nbsp;
<a id="Header1_MyLinks1_HomeLink" href="http://www.cnblogs.com/">博客园</a> ::
<a id="Header1_MyLinks1_MyHomeLink" href="http://www.cnblogs.com/ilife/">首页</a> ::
<a href="http://q.cnblogs.com" class="menu">博问</a> ::
<a href="http://home.cnblogs.com/ing/" class="menu">闪存</a> ::
<a id="Header1_MyLinks1_NewPostLink" rel="nofollow" href="http://www.cnblogs.com/ilife/admin/EditPosts.aspx?opt=1">新随笔</a> ::
<a id="Header1_MyLinks1_ContactLink" accesskey="9" rel="nofollow" href="http://space.cnblogs.com/msg/send/iLife">联系</a> ::
<a id="Header1_MyLinks1_Syndication" href="http://www.cnblogs.com/ilife/rss">订阅</a>
<a id="Header1_MyLinks1_XMLLink" class="XMLLink" href="http://www.cnblogs.com/ilife/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a> ::
<a id="Header1_MyLinks1_Admin" rel="nofollow" href="http://www.cnblogs.com/ilife/admin/EditPosts.aspx">管理</a> ::

</td>
			<td><img id="Header1_BlueTab" src="/skins/Cogitation/Images/BlueTabRight.gif" align="absmiddle" /></td>
			<td class="HeaderBarTabBack" nowrap width="100%">
				
<div class="BlogStatsBar">
	<table class="BlogStatsBar">
		<tr>
			<td width="100%">
			</td>
			<td class="BlogStatsBar" nowrap>
				&nbsp;
				12 
				随笔&nbsp;::
				0 文章
				::
				0 评论
				::
				0 引用
			</td>
		</tr>
	</table>
</div>

			</td>
		</tr>
	</table>
</div>

</div>
<div id="leftmenu">	

	    <div id="blog-calendar-block" style="display:none"><div id="blog-calendar"></div>
</div>
		
<h3>公告</h3>
<div class="News">
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

		<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
	
</div>
<div id="main">
	

<div class="post">
	<div class="postTitle">
		<a id="cb_post_title_url" href="http://www.cnblogs.com/ilife/archive/2013/02/28/2936067.html">（转）Spring Security 3.1 自定义实例之登陆</a>
	</div>
	
	<div class="postText">
		<div id="cnblogs_post_body"><h3>Spring Security 3.1 自定义实例之登陆</h3>
<div>Spring Security的前身是Acegi，功能强大，配置也比较复杂，我也是新手，先通过动手实现一个登陆验证实例来进入Spring Security的世界吧！</div>
<div>1.准备：</div>
<div>下载Spring Security：官方下载地址：<a href="http://www.springsource.org/spring-security">http://www.springsource.org/spring-security</a>&nbsp;在下载之前Spring会要你填写一些基本信息，然后就会自动跳转到下载地址，<strong>目前最新版本为spring-security-3.1.0.RELEASE</strong>，解压后在dist目录下可以看到：</div>
<div><img src="http://hi.csdn.net/attachment/201202/9/0_1328752156vs0m.gif" alt="" /></div>
<div>其中两个War包是实例，*-source.jar是源代码</div>
<div>将dist中的Jar包全部导入到工程中，如图：</div>
<div><img src="http://hi.csdn.net/attachment/201202/9/0_1328752322NTlf.gif" alt="" /></div>
<div>&nbsp;</div>
<div>2.修改web.xml，插入以下代码</div>
<div>
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools"><strong>[html]</strong>&nbsp;<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xiakan008/article/details/7244316">view plain</a><a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xiakan008/article/details/7244316">copy</a><a class="PrintSource" title="print" href="http://blog.csdn.net/xiakan008/article/details/7244316">print</a><a class="About" title="?" href="http://blog.csdn.net/xiakan008/article/details/7244316">?</a>
<div><object id="ZeroClipboardMovie_1" width="29" height="14" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" /><param name="loop" value="loop" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="false" /><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" /><param name="flashvars" value="id=1&amp;width=29&amp;height=14" /><param name="wmode" value="transparent" /></object></div>
</div>
</div>
<ol class="dp-xml" start="1">
<li class="alt"><span><span>&nbsp;&nbsp;</span><span class="tag">&lt;</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>springSecurityFilterChain</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>org.springframework.web.filter.DelegatingFilterProxy</span><span class="tag">&lt;/</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;</span></li>
<li><span>&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>springSecurityFilterChain</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>/*</span><span class="tag">&lt;/</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
</ol></div>
修改context-param项，目的就是带起接下来要新建的AppSecurity.xml(名字随意)</div>
<div>
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools"><strong>[html]</strong>&nbsp;<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xiakan008/article/details/7244316">view plain</a><a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xiakan008/article/details/7244316">copy</a><a class="PrintSource" title="print" href="http://blog.csdn.net/xiakan008/article/details/7244316">print</a><a class="About" title="?" href="http://blog.csdn.net/xiakan008/article/details/7244316">?</a>
<div><object id="ZeroClipboardMovie_2" width="29" height="14" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" /><param name="loop" value="loop" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="false" /><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" /><param name="flashvars" value="id=2&amp;width=29&amp;height=14" /><param name="wmode" value="transparent" /></object></div>
</div>
</div>
<ol class="dp-xml" start="1">
<li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">context-param</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">param-name</span><span class="tag">&gt;</span><span>contextConfigLocation</span><span class="tag">&lt;/</span><span class="tag-name">param-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">param-value</span><span class="tag">&gt;</span><span>classpath*:App*.xml</span><span class="tag">&lt;/</span><span class="tag-name">param-value</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;/</span><span class="tag-name">context-param</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
</ol></div>
<br />3.新建AppSecurity.xml，如下：</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;?</span><span style="color: #ff00ff;">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff;">?&gt;</span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">beans:beans </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.springframework.org/schema/security"</span><span style="color: #ff0000;"> &nbsp;
</span><span style="color: #008080;"> 3</span> <span style="color: #ff0000;">    xmlns:xsi</span><span style="color: #0000ff;">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000;"> &nbsp;
</span><span style="color: #008080;"> 4</span> <span style="color: #ff0000;">    xmlns:beans</span><span style="color: #0000ff;">="http://www.springframework.org/schema/beans"</span><span style="color: #ff0000;"> &nbsp;
</span><span style="color: #008080;"> 5</span> <span style="color: #ff0000;">    xsi:schemaLocation</span><span style="color: #0000ff;">="
</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">&nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">&nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans/spring-beans.xsd
</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">&nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/security
</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">&nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/security/spring-security-3.1.xsd"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">10</span> <span style="color: #000000;">    &nbsp;&nbsp;
</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http </span><span style="color: #ff0000;">auto-config</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">12</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/*"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="ROLE_USER"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">13</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">http</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">14</span> 
<span style="color: #008080;">15</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-manager </span><span style="color: #ff0000;">alias</span><span style="color: #0000ff;">="authenticationManager"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">16</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-provider</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">17</span>             <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">user-service</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">18</span>                 <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">user </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="test"</span><span style="color: #ff0000;"> password</span><span style="color: #0000ff;">="test"</span><span style="color: #ff0000;"> authorities</span><span style="color: #0000ff;">="ROLE_USER"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">19</span>             <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">user-service</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">20</span>         <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">authentication-provider</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">21</span> <span style="color: #000000;">        &nbsp; &nbsp;
</span><span style="color: #008080;">22</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">authentication-manager</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">23</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">beans:beans</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
&nbsp;</div>
<div data-find="_1">

这个xml文件里用了Spring Security的最简单配置&nbsp;&lt;http auto-config="true"/&gt;含义就是使用Spring Security自动为我们设置一些参数,然后声明了访问所有URL都需要为ROLE_USER，然后在authentication-manager用硬编码的形式定义了一个用户test，权限为ROLE_USER，运行这个实例应该可以看到这样的登录界面：</div>
<div><img src="http://hi.csdn.net/attachment/201202/9/0_1328754199qi72.gif" alt="" /></div>
<div>这个界面就是Spring&nbsp;Security为我们提供的登陆界面；</div>
<div>上面这个实例太简单了，在现实项目中基本不会这么用，比如项目如有如下需求：</div>
<div><strong>1.自定义登录界面；</strong></div>
<div><strong>2.用Ajax方式登录验证；</strong></div>
<div><strong>3.用数据库记录用户信息，进行登录验证；</strong></div>
<div><strong>4.用MD5方式密码加密；</strong></div>
<div>OK 接下来我们先来看看<strong>自定义登录界面</strong>的配置实例吧！</div>
<div>1.修改AppSecurity.xml中的http项，如下：</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: #008080;">1</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http </span><span style="color: #ff0000;">auto-config</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">2</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/login.jsp"</span><span style="color: #ff0000;"> filters</span><span style="color: #0000ff;">="none"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">3</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/*"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="ROLE_USER"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">4</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">form-login </span><span style="color: #ff0000;">login-page</span><span style="color: #0000ff;">="/login.jsp"</span><span style="color: #ff0000;"> authentication-failure-url</span><span style="color: #0000ff;">="/login.jsp?error=true"</span><span style="color: #ff0000;"> default-target-url</span><span style="color: #0000ff;">="/index.jsp"</span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">5</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">http</span><span style="color: #0000ff;">&gt;</span> </pre>
</div>
</div>
<div>
<div>login-page：当用户登录时显示自定义登录页面&nbsp;<br />authentication-failure-url：登录失败时跳转到哪个页面&nbsp;<br />default-target-url：登录成功后跳转到哪个页面&nbsp;</div>


这样就把默认的登录界面指向了login.jsp了，并且配置了出错后的指向路径和默认跳转路径等信息,当然这里的login.jsp需要任何人都可以访问，不然的话会造成循环调用</div>
<div>2.设计login.jsp，关键代码如下：</div>
<div data-find="_10">
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools"><strong>[html]</strong>&nbsp;<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xiakan008/article/details/7244316">view plain</a><a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xiakan008/article/details/7244316">copy</a><a class="PrintSource" title="print" href="http://blog.csdn.net/xiakan008/article/details/7244316">print</a><a class="About" title="?" href="http://blog.csdn.net/xiakan008/article/details/7244316">?</a>
<div><object id="ZeroClipboardMovie_5" width="29" height="14" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" /><param name="loop" value="loop" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="false" /><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" /><param name="flashvars" value="id=5&amp;width=29&amp;height=14" /><param name="wmode" value="transparent" /></object></div>


</div>


</div>
<ol class="dp-xml" start="1">
<li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">html</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;</span><span class="tag-name">head</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">meta</span><span>&nbsp;</span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span>&nbsp;</span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html;&nbsp;charset=utf-8"</span><span>&nbsp;</span><span class="tag">/&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;</span><span class="tag-name">title</span><span class="tag">&gt;</span><span>登陆界面</span><span class="tag">&lt;/</span><span class="tag-name">title</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">head</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;</span><span class="tag-name">body</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">h3</span><span class="tag">&gt;</span><span>欢迎登陆</span><span class="tag">&lt;/</span><span class="tag-name">h3</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;</span><span class="tag-name">form</span><span>&nbsp;</span><span class="attribute">action</span><span>=</span><span class="attribute-value">'/ctosii_middle/j_spring_security_check'</span><span>&nbsp;</span><span class="attribute">method</span><span>=</span><span class="attribute-value">'POST'</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;<span class="tag">&lt;</span><span class="tag-name">table</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">td</span><span class="tag">&gt;</span><span>用户名:</span><span class="tag">&lt;/</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">input</span><span>&nbsp;</span><span class="attribute">type</span><span>=</span><span class="attribute-value">'text'</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">'j_username'</span><span>&nbsp;</span><span class="attribute">value</span><span>=</span><span class="attribute-value">''</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">td</span><span class="tag">&gt;</span><span>密&nbsp;&nbsp;&nbsp;&nbsp;码:</span><span class="tag">&lt;/</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">input</span><span>&nbsp;</span><span class="attribute">type</span><span>=</span><span class="attribute-value">'password'</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">'j_password'</span><span class="tag">/&gt;</span><span class="tag">&lt;/</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">td</span><span>&nbsp;</span><span class="attribute">colspan</span><span>=</span><span class="attribute-value">'2'</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">input</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"submit"</span><span>&nbsp;</span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span>&nbsp;</span><span class="attribute">value</span><span>=</span><span class="attribute-value">"登陆"</span><span class="tag">/&gt;</span><span class="tag">&lt;/</span><span class="tag-name">td</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">tr</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span>&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">table</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;/</span><span class="tag-name">form</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">body</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
<li><span><span class="tag">&lt;/</span><span class="tag-name">html</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>


</ol></div>


大家可以看到这里的action指向了j_spring_security_check,这个路径也是Spring Security为我们自动生成的，这里需要post的两个个参数为<strong>j_username,j_password，</strong>运行这个实例可以看到如下登陆界面：</div>
<div><img src="http://hi.csdn.net/attachment/201202/9/0_13287567100UNP.gif" alt="" /></div>
<div>2.用ajax的方式实现的登录验证</div>
<div>Spring Security默认的是登陆验证通过用跳转的方式，但是在实际应用中很多应用的登录界面需要用到ajax的方式来实现登陆验证</div>
<div>1.修改AppSecurity.xml中的http项，如下：</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: #008080;">1</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http </span><span style="color: #ff0000;">auto-config</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">2</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/login**"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="IS_AUTHENTICATED_ANONYMOUSLY"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">3</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/**/*.js"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="IS_AUTHENTICATED_ANONYMOUSLY"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">4</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/**"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="ROLE_USER"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">5</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">form-login </span><span style="color: #ff0000;">login-page</span><span style="color: #0000ff;">="/login.jsp"</span><span style="color: #ff0000;"> authentication-failure-url</span><span style="color: #0000ff;">="/login_fail.do"</span><span style="color: #ff0000;"> default-target-url</span><span style="color: #0000ff;">="/login_success.do"</span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">6</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">http</span><span style="color: #0000ff;">&gt;</span>&nbsp;</pre>
</div>
<p>这里配置了两个路径失败后的调用路径和成功后的调用路径。利用这两个路径可以实现ajax登陆。<br />在login_fail.do和login_success.do中可以输出登陆失败、成功的消息</p>
<p>2.Login.jsp修改如下：</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=utf-8"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;"> 4</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">="js/jquery-last.min.js"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;"> 5</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;"> 6</span> <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> doLogin(){
</span><span style="color: #008080;"> 7</span>     <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> url </span><span style="background-color: #f5f5f5; color: #000000;">=</span> <span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">/ctosii_middle/j_spring_security_check</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">;
</span><span style="color: #008080;"> 8</span>     <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> username</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> $(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">input[name='j_username']</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).val();
</span><span style="color: #008080;"> 9</span>     <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> password</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> $(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">input[name='j_password']</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).val();
</span><span style="color: #008080;">10</span> <span style="background-color: #f5f5f5; color: #000000;">    $.ajax({
</span><span style="color: #008080;">11</span> <span style="background-color: #f5f5f5; color: #000000;">        url:url,
</span><span style="color: #008080;">12</span> <span style="background-color: #f5f5f5; color: #000000;">        type:</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">POST</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">,
</span><span style="color: #008080;">13</span> <span style="background-color: #f5f5f5; color: #000000;">        data:</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">j_username=</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">+</span><span style="background-color: #f5f5f5; color: #000000;">username</span><span style="background-color: #f5f5f5; color: #000000;">+</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">&amp;j_password=</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">+</span><span style="background-color: #f5f5f5; color: #000000;">password,
</span><span style="color: #008080;">14</span> <span style="background-color: #f5f5f5; color: #000000;">        success: </span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;">(data){
</span><span style="color: #008080;">15</span> <span style="background-color: #f5f5f5; color: #000000;">            $(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">#results</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).text(data);
</span><span style="color: #008080;">16</span> <span style="background-color: #f5f5f5; color: #000000;">          }
</span><span style="color: #008080;">17</span> <span style="background-color: #f5f5f5; color: #000000;">    });
</span><span style="color: #008080;">18</span> <span style="background-color: #f5f5f5; color: #000000;">}
</span><span style="color: #008080;">19</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">20</span> 
<span style="color: #008080;">21</span> 
<span style="color: #008080;">22</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span>登陆界面<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">23</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">24</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">25</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">&gt;</span>欢迎登陆<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">26</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">form</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">27</span>  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">table</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">28</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;</span>用户名:<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">='text' </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">='j_username' </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">=''</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">29</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;</span>密    码:<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">='password' </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">='j_password'</span><span style="color: #0000ff;">/&gt;&lt;/</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">30</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">td </span><span style="color: #ff0000;">colspan</span><span style="color: #0000ff;">='2'</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="button"</span><span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="doLogin()"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="登陆"</span><span style="color: #0000ff;">/&gt;&lt;/</span><span style="color: #800000;">td</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">31</span>   <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">table</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">32</span>   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="results"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">33</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">form</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">34</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">35</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<p>3.用数据库记录用户信息，进行登录验证</p>
<p>上面的两个例子都是用了硬编码的方式验证用户信息，而实际项目中肯定是会把用户信息存到数据库中的，最典型的就是建users,role,user_role这三张表：</p>
<p>users:</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">TABLE</span> <span style="color: #0000ff;">IF</span> <span style="color: #808080;">EXISTS</span><span style="color: #000000;"> `users`;
</span><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span><span style="color: #000000;"> `users` (
  `id` </span><span style="color: #0000ff;">int</span>(<span style="color: #800000; font-weight: bold;">11</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;"> AUTO_INCREMENT,
  `username` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">20</span>) <span style="color: #0000ff;">CHARACTER</span> <span style="color: #0000ff;">SET</span> utf8 <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
  `password` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">100</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
  `enabled` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">1</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">DEFAULT</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">'</span><span style="color: #000000;">,
  </span><span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span><span style="color: #000000;"> (`id`)
) ENGINE</span><span style="color: #808080;">=</span>InnoDB AUTO_INCREMENT<span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">2</span> <span style="color: #0000ff;">DEFAULT</span> CHARSET<span style="color: #808080;">=</span>latin1;</pre>
</div>
<p>role:</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">TABLE</span> <span style="color: #0000ff;">IF</span> <span style="color: #808080;">EXISTS</span><span style="color: #000000;"> `role`;
</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span><span style="color: #000000;"> `role` (
</span><span style="color: #008080;">3</span>   `id` <span style="color: #0000ff;">int</span>(<span style="color: #800000; font-weight: bold;">11</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;"> AUTO_INCREMENT,
</span><span style="color: #008080;">4</span>   `name` <span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">100</span>) <span style="color: #0000ff;">CHARACTER</span> <span style="color: #0000ff;">SET</span> utf8 <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
</span><span style="color: #008080;">5</span>   `description` <span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">255</span>) <span style="color: #0000ff;">CHARACTER</span> <span style="color: #0000ff;">SET</span> utf8 <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
</span><span style="color: #008080;">6</span>   `enabled` <span style="color: #0000ff;">tinyint</span>(<span style="color: #800000; font-weight: bold;">1</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
</span><span style="color: #008080;">7</span>   <span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span><span style="color: #000000;"> (`id`),
</span><span style="color: #008080;">8</span>   <span style="color: #0000ff;">UNIQUE</span> <span style="color: #0000ff;">KEY</span><span style="color: #000000;"> `rolename_index` (`name`) USING BTREE
</span><span style="color: #008080;">9</span> ) ENGINE<span style="color: #808080;">=</span>InnoDB AUTO_INCREMENT<span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">3</span> <span style="color: #0000ff;">DEFAULT</span> CHARSET<span style="color: #808080;">=</span>latin1;</pre>
</div>
<p>user_role:</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">TABLE</span> <span style="color: #0000ff;">IF</span> <span style="color: #808080;">EXISTS</span><span style="color: #000000;"> `users`;
</span><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span><span style="color: #000000;"> `users` (
  `id` </span><span style="color: #0000ff;">int</span>(<span style="color: #800000; font-weight: bold;">11</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;"> AUTO_INCREMENT,
  `username` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">20</span>) <span style="color: #0000ff;">CHARACTER</span> <span style="color: #0000ff;">SET</span> utf8 <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
  `password` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">100</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">,
  `enabled` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">1</span>) <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">DEFAULT</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">'</span><span style="color: #000000;">,
  </span><span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span><span style="color: #000000;"> (`id`)
) ENGINE</span><span style="color: #808080;">=</span>InnoDB AUTO_INCREMENT<span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">2</span> <span style="color: #0000ff;">DEFAULT</span> CHARSET<span style="color: #808080;">=</span>latin1;</pre>
</div>
<p>然后执行Sql插入数据：</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> `users` <span style="color: #0000ff;">VALUES</span> (<span style="color: #ff0000;">'</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">xiakan</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">xiakan</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">1</span><span style="color: #ff0000;">'</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> `role` <span style="color: #0000ff;">VALUES</span> (<span style="color: #ff0000;">'</span><span style="color: #ff0000;">1</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">user</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">用户角色</span><span style="color: #ff0000;">'</span>, <span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> `role` <span style="color: #0000ff;">VALUES</span> (<span style="color: #ff0000;">'</span><span style="color: #ff0000;">2</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">admin</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">管理员角色</span><span style="color: #ff0000;">'</span>, <span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> `user_role` <span style="color: #0000ff;">VALUES</span> (<span style="color: #ff0000;">'</span><span style="color: #ff0000;">1</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">1</span><span style="color: #ff0000;">'</span>, <span style="color: #ff0000;">'</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">'</span>);</pre>
</div>
<p>2.修改AppSecurity.xml</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;?</span><span style="color: #ff00ff;">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff;">?&gt;</span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">beans:beans </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.springframework.org/schema/security"</span>
<span style="color: #008080;"> 3</span> <span style="color: #ff0000;">       xmlns:xsi</span><span style="color: #0000ff;">="http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #008080;"> 4</span> <span style="color: #ff0000;">       xmlns:beans</span><span style="color: #0000ff;">="http://www.springframework.org/schema/beans"</span>
<span style="color: #008080;"> 5</span> <span style="color: #ff0000;">       xsi:schemaLocation</span><span style="color: #0000ff;">="
</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">       http://www.springframework.org/schema/beans
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">       http://www.springframework.org/schema/beans/spring-beans.xsd
</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">       http://www.springframework.org/schema/security
</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">       http://www.springframework.org/schema/security/spring-security-3.1.xsd"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">10</span>     
<span style="color: #008080;">11</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http </span><span style="color: #ff0000;">auto-config</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">12</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/login**"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="IS_AUTHENTICATED_ANONYMOUSLY"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">13</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/**/*.js"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="IS_AUTHENTICATED_ANONYMOUSLY"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">14</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">intercept-url </span><span style="color: #ff0000;">pattern</span><span style="color: #0000ff;">="/**"</span><span style="color: #ff0000;"> access</span><span style="color: #0000ff;">="ROLE_USER"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">15</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">form-login </span><span style="color: #ff0000;">login-page</span><span style="color: #0000ff;">="/login.jsp"</span><span style="color: #ff0000;"> authentication-failure-url</span><span style="color: #0000ff;">="/login_fail.do"</span><span style="color: #ff0000;"> default-target-url</span><span style="color: #0000ff;">="/login_success.do"</span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">16</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">http</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">17</span>       
<span style="color: #008080;">18</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">beans:bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="userDetailsService"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="com.xk.security.UserDetailServiceImpl"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">19</span>     
<span style="color: #008080;">20</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-manager </span><span style="color: #ff0000;">alias</span><span style="color: #0000ff;">="authenticationManager"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">21</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-provider </span><span style="color: #ff0000;">user-service-ref</span><span style="color: #0000ff;">="userDetailsService"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #008080;">22</span>     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">authentication-manager</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #008080;">23</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">beans:beans</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<p>我们添加了一个Bean:com.xk.security.UserDetailServiceImpl，这个bean的代码如下：</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> com.xk.security;
</span><span style="color: #008080;"> 2</span> 
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.ArrayList;
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List;
</span><span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.beans.factory.annotation.Autowired;
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.security.core.GrantedAuthority;
</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.security.core.authority.SimpleGrantedAuthority;
</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.security.core.userdetails.UserDetails;
</span><span style="color: #008080;">10</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.security.core.userdetails.UserDetailsService;
</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.security.core.userdetails.UsernameNotFoundException;
</span><span style="color: #008080;">12</span> 
<span style="color: #008080;">13</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> com.xk.dao.IUserManager;
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> com.xk.item.Role;
</span><span style="color: #008080;">15</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> com.xk.item.User;
</span><span style="color: #008080;">16</span> 
<span style="color: #008080;">17</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> UserDetailServiceImpl <span style="color: #0000ff;">implements</span><span style="color: #000000;"> UserDetailsService{
</span><span style="color: #008080;">18</span> <span style="color: #000000;">    @Autowired
</span><span style="color: #008080;">19</span>     <span style="color: #0000ff;">private</span> IUserManager userManager = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">20</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> UserDetailServiceImpl(){
</span><span style="color: #008080;">21</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">22</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> IUserManager getUserManager() {
</span><span style="color: #008080;">23</span>         <span style="color: #0000ff;">return</span><span style="color: #000000;"> userManager;
</span><span style="color: #008080;">24</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">25</span>     <span style="color: #0000ff;">public</span> UserDetails loadUserByUsername(String username) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> UsernameNotFoundException {
</span><span style="color: #008080;">26</span>         User user =<span style="color: #000000;"> userManager.search(username);
</span><span style="color: #008080;">27</span>         <span style="color: #0000ff;">if</span>  (user==<span style="color: #0000ff;">null</span><span style="color: #000000;">)
</span><span style="color: #008080;">28</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> UsernameNotFoundException(username+" not exist!"<span style="color: #000000;">);
</span><span style="color: #008080;">29</span>         List&lt;GrantedAuthority&gt; authsList = <span style="color: #0000ff;">new</span> ArrayList&lt;GrantedAuthority&gt;<span style="color: #000000;">();
</span><span style="color: #008080;">30</span>         <span style="color: #0000ff;">for</span><span style="color: #000000;">  (Role role:userManager.getRoles(user)) {
</span><span style="color: #008080;">31</span>             authsList.add(<span style="color: #0000ff;">new</span><span style="color: #000000;"> SimpleGrantedAuthority(role.getName()));
</span><span style="color: #008080;">32</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">33</span>         <span style="color: #0000ff;">boolean</span> accountNonExpired = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">34</span>         <span style="color: #0000ff;">boolean</span> credentialsNonExpired = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">35</span>         <span style="color: #0000ff;">boolean</span> accountNonLocked = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">36</span> 
<span style="color: #008080;">37</span>         UserDetails userdetails = <span style="color: #0000ff;">new</span><span style="color: #000000;"> org.springframework.security.core.userdetails.User(user.getUsername(), user
</span><span style="color: #008080;">38</span> <span style="color: #000000;">                .getPassword(),user.isEnabled(), accountNonExpired, credentialsNonExpired, accountNonLocked, authsList);
</span><span style="color: #008080;">39</span> 
<span style="color: #008080;">40</span>         <span style="color: #0000ff;">return</span><span style="color: #000000;"> userdetails;
</span><span style="color: #008080;">41</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">42</span> }</pre>
</div>
<p>用刚刚的insert到数据库的用户信息，就可以看到如下界面：</p>
<p>4.用MD5方式密码加密；</p>
</div>
<div>&nbsp;</div>
<div>这里的数据库中用了明码的方式来存储用户密码，这样对用户来说是很不安全的，前段时间各大网站的密码泄露事件就是因为用了明文存储用户密码，为了加密用户的密码，这里我们可以用MD5的方式：</div>
<div>1.修改AppSecurity.xml：</div>
<div>
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">
<div class="cnblogs_code">
<pre>    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-manager </span><span style="color: #ff0000;">alias</span><span style="color: #0000ff;">="authenticationManager"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">authentication-provider </span><span style="color: #ff0000;">user-service-ref</span><span style="color: #0000ff;">="userDetailsService"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">password-encoder </span><span style="color: #ff0000;">hash</span><span style="color: #0000ff;">="md5"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">salt-source </span><span style="color: #ff0000;">user-property</span><span style="color: #0000ff;">="username"</span> <span style="color: #0000ff;">/&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">password-encoder</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">authentication-provider</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">authentication-manager</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
</div>
</div>
</div>
并将xiakan的密码改成65aedbe7fb37dc0b2a0da914fb905328</div>
<div>其实也可以用js做好加密后post到服务器，这样更安全!</div></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


	</div>
	
	<div class="postfoot">
		posted on <span id="post-date">2013-02-28 00:05</span> <a href='http://www.cnblogs.com/ilife/'>iLife</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://www.cnblogs.com/ilife/admin/EditPosts.aspx?postid=2936067" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(2936067);return false;">收藏</a>
	</div>
</div>
<script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=69616,cb_entryId=2936067,cb_blogApp=currentBlogApp,cb_blogUserGuid='a9ccc5d1-f741-df11-ba8f-001cf0cd104b',cb_entryCreatedDate='2013/2/28 0:05:00';loadViewCount(cb_entryId);</script>

<a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id="comment_form" class="commentform">
<a name="commentform"></a>
<div id="divCommentShow"></div>
<div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" id="lnk_RefreshComments" onclick="return RefreshCommentList();">刷新评论</a><a href="#" onclick="return RefreshPage();">刷新页面</a><a href="#top">返回顶部</a></div>
<div id="comment_form_container"></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
<div id="site_nav_under"><a href="http://www.cnblogs.com/" target="_blank" title="程序员的网上家园">博客园首页</a><a href="http://q.cnblogs.com/" target="_blank" title="程序员问答社区">博问</a><a href="http://news.cnblogs.com/" target="_blank" title="IT新闻">新闻</a><a href="http://home.cnblogs.com/ing/" target="_blank">闪存</a><a href="http://job.cnblogs.com/" target="_blank">程序员招聘</a><a href="http://kb.cnblogs.com/" target="_blank">知识库</a></div>
<div id="ad_under_post_holder"></div>
<script type="text/javascript">
var enableGoogleAd = true;
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
fixPostBodyFormat();
loadAdUnderPost();
</script>
<div id="HistoryToday" class="c_ad_block"></div>
<script type="text/javascript">
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
</script>
<script type="text/javascript">
    $.ajax({ url: 'http://counter.cnblogs.com/blog/post/' + cb_entryId, type: 'get', dataType: 'script', cache: true });
</script>
</div>

</div>
<div class="footer">

<p id="footer">
	Powered by: 
	<br />
	
	<a id="Footer1_Hyperlink3" NAME="Hyperlink1" href="http://www.cnblogs.com/" style="font-family:Verdana;font-size:12px;">博客园</a>
	<br />
	Copyright &copy; iLife
</p>
</div>
<script type="text/javascript" src="http://common.cnblogs.com/script/google-analytics.js"></script>
</body>
</html>
